﻿using Bloggie.Web.Data;
using Bloggie.Web.Models.Domain;
using Bloggie.Web.Models.ViewModels;
using Bloggie.Web.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Bloggie.Web.Controllers;

public class AdminTagsController : Controller
{
    private readonly BloggieDbContext _bloggieDbContext;
    private readonly ITagRepository _tagRepository;

    public AdminTagsController(BloggieDbContext bloggieDbContext, ITagRepository tagRepository)
    {
        _bloggieDbContext = bloggieDbContext;
        _tagRepository = tagRepository;
    }


    [HttpGet]
    public IActionResult Add()
    {
        return View();
    }

    [HttpPost]
    [ActionName("Add")]
    public async Task<IActionResult> Add(AddTagRequest addTagRequest)
    {
        // Mapping AddTagRequest to Tag domain model
        var tag = new Tag
        {
            Name = addTagRequest.Name,
            DisplayName = addTagRequest.DisplayName
        };
        await _tagRepository.AddAsync(tag);
        //return View("Add");
        return RedirectToAction("List");
    }

    [HttpGet]
    [ActionName("List")]
    public async Task<IActionResult> List()
    {
        // use dbContext to read the tags 
        //var tags = await _bloggieDbContext.Tags.ToListAsync();
        var tags = await _tagRepository.GetAllAsync();
        return View(tags.ToList());
    }

    [HttpGet]
    public async Task<IActionResult> Edit(Guid id)
    {
        var tag = await _tagRepository.GetAsync(id);
        if (tag != null)
        {
            var editTagRequest = new EditTagRequest
            {
                Id = tag.Id,
                Name = tag.Name,
                DisplayName = tag.DisplayName
            };
            return View(editTagRequest);
        }

        return View(null);
    }

    [HttpPost]
    public async Task<IActionResult> Edit(EditTagRequest editTagRequest)
    {
        var tag = new Tag
        {
            Id = editTagRequest.Id,
            Name = editTagRequest.Name,
            DisplayName = editTagRequest.DisplayName
        };

        var updateTag = await _tagRepository.UpdateAsync(tag);
        if (updateTag != null)
        {
        }
        else
        {
        }

        return RedirectToAction("Edit", new { id = editTagRequest.Id });
    }

    [HttpPost]
    public async Task<IActionResult> Delete(EditTagRequest editTagRequest)
    {
        // var tag = await _bloggieDbContext.Tags.FindAsync(editTagRequest.Id);
        // if (tag != null)
        // {
        //     _bloggieDbContext.Tags.Remove(tag);
        //     await _bloggieDbContext.SaveChangesAsync();
        //     // Show a success notification
        //     return RedirectToAction("List");
        // }

        var delTag = await _tagRepository.DeleteAsync(editTagRequest.Id);
        if (delTag != null)
        {
            // Show a success notification
            return RedirectToAction("List");
        }

        return RedirectToAction("Edit", new { id = editTagRequest.Id });
    }
}